package com.sx.core.utils;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created by lihao on 2017/3/16.
 */
public class SocketUtils {

    public static void main(String[] args){

//        String str=  SocketUtils.request(Constants.hand_ip,Constants.hand_port,"{\"001\":[]}\\r\\n");
//        Map map = JSONUtil.parseObject(str, Map.class);
//        List list=(List)map.get("101");
//        for(int i=0;i<list.size();i++){
//            Map smap=(Map)list.get(i);
//            System.out.println("dd");
//        }

    }

    static Log log= LogFactory.getLog(SocketUtils.class);
    synchronized static public String request(String ip,int port,String param){
        Socket socket = null;
        InetSocketAddress address = null;
        try {
            address = new InetSocketAddress(ip, port);
            socket = new Socket();
            socket.connect(address,5000);
            return request(param,socket);
        } catch (Exception e) {
            log.error("create socket error");
            return null;
        }finally {
            IOUtils.closeQuietly(socket);
        }

    }
    static public String requestUTF8(String ip,int port,String param){
        String str=request(ip, port, param);
        try {
            return new String(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("UnsupportedEncodingException",e);
        }
        return null;
    }
    static public String requestGBK(String ip,int port,String param){
        String str=request(ip, port, param);
        try {
            return new String(str.getBytes("GBK"));
        } catch (UnsupportedEncodingException e) {
            log.error("UnsupportedEncodingException",e);
        }
        return null;
    }


    static public String request(String param, Socket socket){
        OutputStream outputStream=null;
        BufferedReader br = null;
        InputStreamReader reader = null;
        InputStream inputStream=null;
        try{
            outputStream=socket.getOutputStream();
            outputStream.write(param.getBytes("UTF-8"));
            outputStream.flush();
            inputStream=socket.getInputStream();
            reader=new InputStreamReader(inputStream);
            br = new BufferedReader(reader);
            String str = br.readLine();
            return str;
        }catch(Exception e){
            log.error("request",e);
            return null;
        }finally{
            IOUtils.closeQuietly(outputStream);
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(br);
            IOUtils.closeQuietly(socket);
        }
    }
}
